
import { ccs } from "../services";
import { touchExd } from "../TouchExtend";


const {ccclass, property, menu} = cc._decorator;

const ShowType = cc.Enum({
    '显示进度': 1,
    '显示百分比': 2,
});

@ccclass
@menu('扩展组件/ProgressCtrl')
export default class ProgressCtrl extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property(cc.ProgressBar)
    progressBar: cc.ProgressBar = null;

    @property({type:cc.Node, tooltip: '进度节点的父节点必须是进度条'})
    progressNode: cc.Node = null;

    @property(cc.Component.EventHandler)
    eventHandler: cc.Component.EventHandler = null;

    @property({tooltip: '是否拖动进度', visible(){
        return !!this.progressNode;
    }})
    isMoveProgressNode:boolean = false;

    @property({type: cc.Component.EventHandler, visible(){
        return this.isMoveProgressNode;
    }, tooltip: '拖动进度条的回调函数'})
    moveEvent: cc.Component.EventHandler = null;

    @property({type: ShowType})
    showType: number = 1;

    private _maxValue:number = 0;
    private _curValue:number = 0;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        if(!this.progressBar) {
            this.progressBar = this.getComponent(cc.ProgressBar);
        }

        if(this.isMoveProgressNode && this.progressNode) {
            let beginX = 0;
            let beginValue = 0;
            touchExd.touchScale(this.progressNode,(event:cc.Event.EventTouch)=>{
                beginX = event.getLocationX();
                beginValue = this._curValue;
            }, (event:cc.Event.EventTouch)=>{
                this._curValue = beginValue + (event.getLocationX() - beginX) / this.progressBar.totalLength * this._maxValue;
                this._curValue = Math.min(Math.max(this._curValue, 0), this._maxValue);
                this.setPercent(this._curValue, this._maxValue);
                ccs.gf.doEventCallback(this.moveEvent, this._curValue, this._maxValue);
            })
        }
    }

    setPercent(value:number = 0, maxValue:number, text?:string) {
        if(!this.progressBar) {
            this.progressBar = this.getComponent(cc.ProgressBar);
        }
        if(maxValue <= 0) {
            this.progressBar.progress = 0;
        } else {
            this.progressBar.progress = value / maxValue
        }

        this._maxValue = maxValue;
        this._curValue = value;

        if(this.label) {
            if(text) {
                this.label.string = text;
            } else {
                if(this.showType == 1) {
                    this.label.string = Math.floor(value) + '/' + Math.ceil(maxValue);
                } else {
                    this.label.string = Math.floor(this.progressBar.progress * 100) + '%';
                }
            }
        }

        if(this.progressNode) {
            this.progressNode.x = this.progressBar.totalLength * this.progressBar.progress + this.progressBar.barSprite.node.x;
        }
        ccs.gf.doEventCallback(this.eventHandler, this.progressBar.progress);
    }

    setPercentValue(value:number, text?:string) {
        if(!this.progressBar) {
            this.progressBar = this.getComponent(cc.ProgressBar);
        }
        this.progressBar.progress = value;
        if(this.label) {
            if(text) {
                this.label.string = text;
            } else {
                this.label.string = Math.floor(this.progressBar.progress * 100) + '%';
            }
        }
        if(this.progressNode) {
            this.progressNode.x = this.progressBar.totalLength * this.progressBar.progress + this.progressBar.barSprite.node.x;
        }
        ccs.gf.doEventCallback(this.eventHandler, this.progressBar.progress);
    }

    // update (dt) {}
}
